package cn.itlaobing.dao;

import cn.itlaobing.model.DtoTrainModel;
import cn.itlaobing.model.TrainModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by Administrator on 2017-11-09.
 */
public class TrainDao  extends BaseDao {
    public DtoTrainModel QueryAll(String stationNum, String name1, String name2 ) throws SQLException {
        QueryRunner qr=new QueryRunner(getDataSource());
        String sql="SELECT * " +
                "FROM station  " +
                "WHERE trainNum=? " +
                "AND station=?";

        BeanHandler<TrainModel> userBeanHandle=new BeanHandler<TrainModel>(TrainModel.class);
        TrainModel tm1=qr.query(sql, userBeanHandle,stationNum,name1);
        TrainModel tm2=qr.query(sql, userBeanHandle,stationNum,name2);
        DtoTrainModel dtm=null;
        if(tm1!=null&& tm2!=null)
        {
            dtm=new DtoTrainModel();
            dtm.setTrainNum(tm1.getTrainNum());

            boolean flag=tm1.getSitePrice()>tm2.getSitePrice()?true:false;
            if(flag)
            {
                dtm.setStationName1(tm2.getStation());
                dtm.setStationName2(tm1.getStation());
                Float price=tm1.getSitePrice()-tm2.getSitePrice();
                dtm.setSitePrice(price);
            }
            else
            {
                dtm.setStationName1(tm1.getStation());
                dtm.setStationName2(tm2.getStation());
                Float price=tm2.getSitePrice()-tm1.getSitePrice();
                dtm.setSitePrice(price);
            }
        }
        return dtm;


    }
}
